<template>
  <div class="content">

    <div v-if="show.cunblock">
      <div class="title">
        平台用户账号管理
      </div>

      <div class="wrap">

        <div class="head">
          <div class="left">
            <input @keyup.enter="search()" v-model="keywords" placeholder="请输入搜索内容..." type="text">
            <div class="sousuo" @click='search'>
              <img src="../assets/img/sou.png" alt="">
              搜索
            </div>
          </div>

          <div class="add" @click='addUser'>
            添加账号
          </div>
        </div>


        <el-table :data="tableData" border stripe style="width: 100%;margin-top:20px;">
          <el-table-column type="selection" fixed width="55">
          </el-table-column>
          <el-table-column type="index" fixed width="50">
          </el-table-column>
          <el-table-column prop="real_name" label="姓名" width="180">
          </el-table-column>
          <el-table-column prop="mobile" label="联系电话" width="180">
          </el-table-column>
          <el-table-column prop="depar_name" label="部门" width="180">
          </el-table-column>
          <el-table-column prop="weixin" label="微信openid" width="180">
          </el-table-column>
          <el-table-column prop="permiss_name" label="权限" width="180">
          </el-table-column>
          <el-table-column width="180" label="操作">
            <template slot-scope="scope">
              <el-button size="mini" @click='edit(scope.row)'>编辑</el-button>
              <el-button size="mini" @click='delete_account(scope.row)' type="danger">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="page">
          <el-pagination background @current-change='current' @prev-click='prev' @next-click='next' :page-size='page'
            layout="prev, pager, next" :total="count">
          </el-pagination>
        </div>
      </div>
    </div>


    <el-dialog title="编辑" :visible.sync="dialogVisible" width="30%">
      <el-form :model="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="姓名" prop="real_name">
          <el-input v-model="ruleForm.real_name"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="mobile">
          <el-input v-model="ruleForm.mobile"></el-input>
        </el-form-item>
        <el-form-item label="部门">
          <el-select v-model="ruleForm.depar_id" placeholder="请选择">
            <el-option v-for="item in department_select" :key="item.id" :label="item.name" :value="item.id">
            </el-option>

          </el-select>
        </el-form-item>
        <el-form-item label="微信openid" prop="id">
          <el-input></el-input>
        </el-form-item>
        <el-form-item label="权限">
          <el-select v-model="ruleForm.permiss_id" placeholder="请选择" change='change'>
            <el-option v-for="item in limit_select" :key="item.id" :label="item.permiss_name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div class="btn">
        <span @click="submit">提交</span>
        <span @click="cancle">取消</span>
      </div>
    </el-dialog>

    <addUser v-if='show.add' :isshow='show'></addUser>

  </div>
</template>

<script>
  import {
    getaccount_list,
    edit_account,
    permiss_list,
    depar_list,
    account_info,
    delete_account
  } from '@/js/getApi.js'
  import addUser from './add_user.vue'
  export default {
    name: 'b_index',
    components: {
      addUser
    },
    data() {
      return {
        dialogVisible: false,
        keywords: '',
        show: {
          cunblock: true,
          add: false
        },
        ruleForm: {
          token: this.$store.state.token,
          depar_id: '',
          permiss_id: '',
          id: '',
          mobile: '',
          real_name: '',
          type: ''
        },
        tableData: [],
        count: 0,
        page: 8,
        department_select: [], //部门选择
        limit_select: [], //权限选择
      }
    },
    methods: {
      addUser() {
        this.show.cunblock = false;
        this.show.add = true;
      },
      current(val) {
        this.getlist(val, this.page);
      },
      prev(val) {
        this.getlist(val, this.page);
      },
      next(val) {
        this.getlist(val, this.page);
      },
      cancle() {
        this.dialogVisible = false;
      },
      async delete_account(row) {
        const data = await delete_account(this.$store.state.token, row.id);
        alert(data.msg);
        if (data.code == 1) {
          this.getlist(1, this.page);
        }
      },
      //搜索
      async search() {
        const data = await getaccount_list(this.$store.state.token, this.keywords, 1, this.page);
        this.tableData = data.list;
        this.count = data.count;
      },
      async edit(row) {
        this.dialogVisible = true;
        this.ruleForm.id = row.id;
        const depar = await depar_list(this.$store.state.token);
        const permiss = await permiss_list(this.$store.state.token);
        const info = await account_info(this.$store.state.token, row.id);
        this.department_select = depar;
        this.limit_select = permiss;
        if (info.depar_id != 0) {
          this.ruleForm.depar_id = info.depar_id;
        }
        if (info.permiss_id != 0) {
          this.ruleForm.permiss_id = info.permiss_id;
        }
        this.ruleForm.mobile = row.mobile;
        this.ruleForm.real_name = row.real_name;
        this.ruleForm.type = 1;

      },
      async submit() {
        const data = await edit_account(this.ruleForm);
        if (data.code == 1) {
          alert(data.msg);
          this.dialogVisible = false;
          this.getlist();
        } else {
          alert(data.msg)
        }
      },
      async getlist(page, size) {
        const data = await getaccount_list(this.$store.state.token, '', page, size);
        this.count = data.count;
        this.tableData = data.list;
      }
    },
    created() {
      this.getlist(1, this.page)
    }
  }
</script>

<style scoped>
  .title {
    height: 84px;
    line-height: 84px;
    color: rgba(5, 5, 5, 100);
    font-size: 16px;
    text-align: left;
    font-family: SourceHanSansSC-regular;
    border-bottom: 1px solid #F7F7F7;
  }


  .head {
    margin-top: 33px;
    display: flex;
  }

  .head div {
    display: inline-block;
  }

  .head input {
    width: 190px;
    height: 30px;
    border-radius: 10px 0 0 10px;
    padding-left: 10px;
    border: 1px solid rgba(187, 187, 187, 100);
    float: left;
  }

  .sousuo {
    width: 88px;
    height: 34px;
    line-height: 34px;
    background-color: rgba(26, 188, 156, 100);
    text-align: center;
    border: 1px solid rgba(255, 255, 255, 100);
    float: left;
    margin-left: -1px;
    margin-top: -1px;
    color: #fff;
    cursor: pointer;
  }

  .sousuo img {
    width: 19px;
    height: 19px;
    vertical-align: middle;
  }

  .add {
    width: 88px;
    height: 34px;
    line-height: 34px;
    background-color: rgba(26, 188, 156, 100);
    text-align: center;
    border: 1px solid rgba(255, 255, 255, 100);
    color: #fff;
    margin-left: 50px;
    cursor: pointer;
  }


  .page {
    text-align: center;
    margin: 30px auto;
  }

  .btn {
    margin-left: 100px;
    margin-top: 100px;
  }

  .btn span {
    display: inline-block;
    cursor: pointer;
  }

  .btn span:nth-of-type(1) {
    width: 100px;
    height: 34px;
    line-height: 34px;
    border-radius: 2px;
    background-color: rgba(26, 188, 156, 100);
    color: rgba(255, 255, 255, 100);
    font-size: 14px;
    text-align: center;
    font-family: Roboto;
  }

  .btn span:nth-of-type(2) {
    width: 100px;
    height: 34px;
    line-height: 34px;
    border-radius: 2px;
    background-color: rgba(255, 255, 255, 100);
    color: rgba(26, 188, 156, 100);
    font-size: 14px;
    text-align: center;
    font-family: Roboto;
    border: 1px solid rgba(26, 188, 156, 100);
    margin-left: 20px;
  }
</style>
